WEBVTT

00:00.990 --> 00:04.640
Welcome back to part two of our introduction to lists.

00:04.710 --> 00:10.650
So in the last session we learned about the general structure of lists and how to create lists.

00:10.680 --> 00:17.100
So now in this session we learn how to select single and multiple element elements from a list and how

00:17.100 --> 00:21.000
to manipulate and change elements of a list.

00:21.030 --> 00:27.480
And we also learn how to slice lists and make some selection of lists.

00:27.600 --> 00:28.170
So what now.

00:28.170 --> 00:31.470
Let's start with our lists which we already know

00:36.540 --> 00:43.630
in our first task is we want to select the very first element of this list and the general rule is that

00:43.660 --> 00:45.810
elements of a list can be assessed.

00:45.820 --> 00:54.100
This sped up records so we take our spot bracket and let's try to get them the very first element of

00:54.100 --> 00:54.670
the list.

00:54.760 --> 01:00.220
So you type in 1 and let's see what we get so we get two.

01:00.250 --> 01:02.150
So two is.

01:02.250 --> 01:02.520
Yeah.

01:02.530 --> 01:04.260
Actually the second element.

01:05.410 --> 01:08.590
So how do we get now our first element.

01:08.580 --> 01:14.260
And yeah the others actually that we have to do type in 0 as index.

01:14.260 --> 01:16.230
And let us see what we get.

01:16.240 --> 01:20.700
So if we're type in 0 to get the first element here it's 1.

01:21.840 --> 01:25.720
And this is neither a mistake or our special last change behavior.

01:25.740 --> 01:33.910
Python Yeah it's a common commonly seen pattern in programming or coding that you have the so-called

01:33.950 --> 01:35.620
zero based indexing.

01:36.070 --> 01:42.570
So the very first element of a sequence has the index Position ZERO.

01:42.760 --> 01:49.240
And then the second tests the index position one the third element has the index to show position 2

01:49.240 --> 01:50.240
and so on.

01:50.230 --> 01:55.120
Now this is a general rule in many programming languages.

01:55.120 --> 01:58.540
So once again it's called zero based indexing.

01:58.540 --> 01:58.870
All right.

01:58.870 --> 02:02.300
So what if now we want to get the fifth element of our list.

02:02.310 --> 02:05.130
So the fifth element is five.

02:05.140 --> 02:10.330
So consequently we have to uh input the index position far.

02:11.080 --> 02:13.840
And now it should return us five.

02:14.200 --> 02:14.980
And this is true.

02:15.850 --> 02:23.880
Alternatively we can also assess the very last element of a sequence by typing the index position minus

02:23.880 --> 02:29.600
one so minus one gets us the very last element of a sequence.

02:29.610 --> 02:38.180
So in this case it's five and the second very last element of our list can be assessed by the index

02:38.180 --> 02:41.220
position minus two.

02:41.450 --> 02:47.330
And consequently the first element of our list can also be indexed by minus five

02:51.320 --> 02:55.100
so that two ways to two index our lists.

02:55.220 --> 02:58.370
So that's the zero based indexing starting from zero.

02:58.370 --> 03:11.710
So our element 1 is has an exposition 0 in exposition 1 in exposition 2 index position 4 sorry in exposition

03:11.720 --> 03:20.900
3 and index position 4 and the alternative phase would be to have index position minus 1 index position

03:20.900 --> 03:30.780
minus 2 index position minus 3 index position minus 4 and index position minus 5.

03:30.830 --> 03:36.280
So and this holds true fire almost every kind of sequence.

03:36.280 --> 03:42.600
So not only false but also for two tuples which we see later off on umpire race and so on.

03:42.620 --> 03:47.170
So yeah this is a very important and basic basic rule here.

03:47.180 --> 03:54.070
How to to assess elements of a sequence and what you can also do you can do it the other way around.

03:54.220 --> 04:03.460
So you can foreign give given element let's say 5 percent can give you the index position so and um

04:03.760 --> 04:09.130
you need to the method index and let's type in 5.

04:09.130 --> 04:19.270
So we would expect B five is at index position far so we would expect here to return for and that's

04:19.270 --> 04:20.800
correct.

04:20.800 --> 04:22.570
Lists are generally mutable.

04:22.570 --> 04:24.020
So what does it mean mutable.

04:24.040 --> 04:26.850
So you can change elements of Alice.

04:27.250 --> 04:29.150
And how do we do this.

04:29.200 --> 04:35.800
So we first have to select our element here we want to select our very the very first element that index

04:35.800 --> 04:41.960
position zero and then we assign with the equals sign a new value here it's hundred.

04:42.430 --> 04:43.720
So let's see what we get.

04:45.460 --> 04:47.320
And here you can see now our lists.

04:47.380 --> 04:49.450
The first element is a hundred and not one.

04:49.450 --> 04:56.710
So we simply changed the first element at an exposure Position ZERO and we can also change the very

04:56.710 --> 04:59.620
last element in our sequence.

04:59.830 --> 05:04.400
So l at an exposition minus one equals hundred one.

05:04.680 --> 05:06.460
Let's see if it works.

05:07.360 --> 05:08.560
And yes it works.

05:08.560 --> 05:14.980
Now what if we want to select many elements of a list and we can do this with a so-called slicing by

05:14.980 --> 05:17.980
using a colon inside the spout brackets.

05:17.980 --> 05:24.130
So in this example here we have a slice from the second element that in exposition one.

05:24.150 --> 05:33.190
And by definition this element is including so from first position unto the fourth index position but

05:33.550 --> 05:38.080
here excluding so let's see what happens.

05:38.110 --> 05:40.350
So we have two three and four.

05:40.360 --> 05:46.990
So we are slicing the list from index position 1 including until index position 4 excluding.

05:47.200 --> 05:48.910
So let's check again here.

05:48.970 --> 05:49.870
Let's see at the list.

05:49.870 --> 06:00.430
So here is an exposition one including until an exposition for but excluding so we get to three and

06:00.430 --> 06:00.820
four

06:03.650 --> 06:07.040
and we can also slice from index position 1 to index position five.

06:07.050 --> 06:10.070
So here in our list there is no index position 5.

06:10.320 --> 06:17.230
So by default we get all elements from index position 1 until the very end.

06:17.430 --> 06:22.700
So let's check and we get to 3 4 and yeah here 101.

06:22.800 --> 06:30.530
And um the alternative way to slice until the very last element including the last element is to to

06:30.540 --> 06:33.990
have just one colon and then nothing

06:38.940 --> 06:44.730
so this means we are slicing from one until the very last element including the last element.

06:44.730 --> 06:48.390
And we can also slice from our very first element.

06:48.480 --> 06:56.850
So at an exposition 0 2 let's say in exposition 3 excluding let's see what we get.

06:56.870 --> 07:03.460
So one hundred two and three as we have including the very first element and the index position 3 so

07:03.470 --> 07:05.860
0 1 2 3.

07:05.880 --> 07:10.470
But excluding so one hundred two and three and the alternative way to do this.

07:10.480 --> 07:18.000
So slicing from the very first element to let's say the element that anyway in exposition 3 is to have

07:18.000 --> 07:20.570
just hear nothing.

07:20.590 --> 07:23.800
Colon 3.

07:23.980 --> 07:26.220
So this is the same actually.

07:26.260 --> 07:27.750
So on one more example.

07:27.760 --> 07:31.440
So let's slice from index position 1 including 2.

07:31.450 --> 07:43.060
Next position 3 excluding and we get 2 and 3 and we can also select all elements by just having a colon

07:43.150 --> 07:51.440
and on the left side of the colon on the right side of the colon nothing gives less than the whole list.

07:52.360 --> 07:55.850
All right so let's enlarge our list with some more elements.

07:55.850 --> 08:03.940
So from 1 to 10 and what we can also do we can choose every second element of our list and we can do

08:03.940 --> 08:06.950
this by having uh two colons.

08:07.010 --> 08:13.900
Um this gives us here every second element beginning from the first including so we have fear starting

08:13.900 --> 08:17.350
from the very first element every second element.

08:17.350 --> 08:28.800
So 1 3 5 7 and 9 and we can also select every third element beginning from the first element by having

08:28.860 --> 08:32.070
double colon and then every three

08:36.360 --> 08:41.820
so we are starting from the first element and then selecting every third element.

08:41.820 --> 08:53.370
So we have 1 4 7 and 10 and we can also start with.

08:53.410 --> 09:01.510
For example the second element in exposition 1 and then select every second element by defining here.

09:01.580 --> 09:09.730
Okay let's start from the second element add an exposition 1 but please only give me every second element.

09:11.110 --> 09:14.530
So we get 2 4 6 8 and 10.

09:14.530 --> 09:17.180
So we are starting from the second element.

09:18.380 --> 09:20.680
And then slicing every second element.

09:20.720 --> 09:29.200
So we have to for 6 8 and 10.

09:29.370 --> 09:31.040
So let's start again with ETL.

09:31.470 --> 09:32.280
That's our list.

09:32.370 --> 09:34.210
So from one to 10.

09:34.320 --> 09:38.160
And we can also assign many new value values to many elements.

09:38.160 --> 09:46.410
So for example if I want to assign to the element that takes position for 50 and to the element that

09:46.410 --> 09:54.570
an exposition 5 51 and to the element that an exposition 652 I can do this by slicing L and assigning

09:54.570 --> 09:57.570
the new values which are here in the list

10:00.730 --> 10:11.110
so as you can see here we have at an exposition 5 or so 0 1 2 3 4 we are 50 50 one that the next position

10:11.110 --> 10:14.060
5 and 50 two at an exposition 6.

10:14.080 --> 10:14.410
All right.

10:14.400 --> 10:17.670
So we cannot do as these so-called broadcasting.

10:17.920 --> 10:25.150
So if we want to have that an exposition for five and six if we want to have 50 then our code as we

10:25.150 --> 10:26.680
have it here is not working.

10:28.720 --> 10:30.040
So we get an error message.

10:30.070 --> 10:36.970
And alternatively how we have to assign two uh in exposition 450 550 and 650.

10:37.240 --> 10:38.680
So we have to do it manually

10:42.160 --> 10:49.540
and then we have 50 at an exposition for five and six alternatively we could have created a list of

10:49.750 --> 10:50.960
three times 50.

10:51.280 --> 10:57.910
So that's that's not out of fear and what we can also do we can delete an element at a certain index

10:57.950 --> 11:03.530
processional so let's say we want to delete them the very first element that in the next position zero.

11:03.560 --> 11:10.830
We can do this and as you can see here the first element so one is removed.

11:10.890 --> 11:15.070
Another thing I want to show you is a nested list or lists of lists.

11:15.320 --> 11:22.580
So the last session we learned that we can actually put any data type into a list and this holds also

11:22.580 --> 11:28.310
true for our list itself so you can just put one on many lists into a list.

11:28.310 --> 11:32.970
And for example we have here a list of let's say football players.

11:32.990 --> 11:40.340
So in each football player has a name and if for example as the height one point eighty six metres and

11:40.400 --> 11:46.550
has a rate so eighty two kilograms and we have four players on our list then for each player we have

11:46.910 --> 11:56.030
one list with the name length and the height so we can create the list and then we have the list with.

11:56.260 --> 12:03.190
So it's a nested list with the four elements and each element is the list itself and we can also print

12:04.170 --> 12:07.320
the type and the length of the list.

12:07.350 --> 12:18.900
Let's do this so we have of course this is a list and it has four elements and we can also slice or

12:18.900 --> 12:25.060
select elements off our list so we can select the very first element at an exposition zero.

12:25.080 --> 12:26.850
So I would expect to get them.

12:26.850 --> 12:27.150
Yeah.

12:27.190 --> 12:27.990
And here

12:31.710 --> 12:32.330
exactly.

12:32.340 --> 12:38.420
So we get we have high end video and use one point it's six meters tall and yes eighty two kilograms.

12:38.490 --> 12:46.600
You can also get the very last player at an exposition three or minus one as you like.

12:46.740 --> 12:52.870
So we get Mike so now and but we can also do we can select the height off from the very first player.

12:52.880 --> 12:56.610
So let's say Andy and we can do that by.

12:56.640 --> 12:59.760
First of all it's select Yeah.

13:00.260 --> 13:05.600
So now we have a sub selection we fear the subtlest and then we want to select the second element of

13:05.600 --> 13:11.210
the index position one we can do this.

13:11.390 --> 13:13.710
And so we have selected the height of India.

13:13.730 --> 13:15.860
So it's one point eighty six meters.

13:16.190 --> 13:22.790
And what we can also do we can create a list containing only all four names of our players.

13:22.850 --> 13:25.510
So we create a list called names.

13:25.610 --> 13:32.800
We open this up brackets and then we say OK the first element should be the very first player.

13:33.070 --> 13:34.210
And the first element.

13:34.210 --> 13:39.720
So at the next position zero is the name here it's Andy.

13:40.600 --> 13:46.630
And then give us the name of the second player give us the name of the third player.

13:47.110 --> 13:48.580
And the name of the fourth player

13:51.610 --> 13:56.400
so now we have a list containing only the names of our players.

13:56.680 --> 13:59.130
So we have a list of four strings.

13:59.230 --> 14:01.740
And that's another thing I want to show you here.

14:01.840 --> 14:05.080
Let's create a list X with four elements.

14:05.080 --> 14:08.290
And then we create another variable Y.

14:08.500 --> 14:10.130
And we assign x.

14:10.810 --> 14:13.590
So here y plus reference to the same list.

14:13.600 --> 14:19.900
And now if you change the very first element of X let's say x that in exposition 0 is 100

14:23.080 --> 14:26.680
so now the list of X is one hundred two three four.

14:26.680 --> 14:29.180
And now if you are checking again I y y.

14:29.340 --> 14:31.450
So why has also changed.

14:31.510 --> 14:33.220
So why is this the case.

14:33.310 --> 14:40.450
Well and what Tyson does internally here it creates a list object and references it makes a reference

14:40.450 --> 14:42.900
to rival x.

14:43.000 --> 14:51.630
And here we assigning y to X so we are creating a reference from X to the same list object.

14:51.660 --> 14:57.600
So we only have one list object and we have two references so one reference to X and one reference to

14:57.600 --> 14:58.560
Y.

14:58.600 --> 15:07.110
And now if you change our object by changing the first element of X Y references to the changed object

15:07.440 --> 15:10.070
so maybe it was our intention that also y changed.

15:10.080 --> 15:18.480
But what if it's not our intention that Y changes if exchanges and what we can do here is that we say

15:19.670 --> 15:27.480
so let's start again here with X equals one two three far and then we create a copy here of the list

15:27.510 --> 15:30.690
by saying okay please create me a copy of X

15:33.630 --> 15:40.010
and to sign it to y so now Y is referencing to a copy of x.

15:40.040 --> 15:42.200
So not to to here the.

15:42.230 --> 15:43.280
The object itself.

15:43.280 --> 15:50.090
So now we created two objects so we have one object the original one that is referencing two X or Xs

15:50.090 --> 15:57.380
referencing to the object and we have a second object which is actually the same elements but only Y

15:57.380 --> 16:01.410
is referencing to the new through the copy a new object.

16:01.700 --> 16:04.670
And if you change it here X now

16:08.410 --> 16:13.380
so why does not change and that's another way to do this.

16:13.380 --> 16:15.080
So let's start again.

16:15.170 --> 16:17.900
So we we are creating an object.

16:17.990 --> 16:19.970
One two three four and assigning X

16:26.110 --> 16:33.060
so we can also create a copy of X by saying okay please give me all elements of x.

16:33.160 --> 16:37.960
So by writing it here and in this manner we are also creating a copy.

16:38.710 --> 16:45.970
So why is it now referencing to our copy so we have two objects object one referencing two X and object

16:45.970 --> 16:49.470
two we are only at Y it's referencing to it.

16:49.610 --> 16:50.800
Now if you change X

16:54.850 --> 16:56.430
also there's no change with Y.

16:56.440 --> 16:59.530
So we have two alternative solutions where we prevent that.

16:59.530 --> 17:02.760
Also Y is changed if we we change x.

17:03.040 --> 17:03.420
All right.

17:03.430 --> 17:10.150
So at the beginning of the session yeah I said that the elements of sequences can be assessed with spec

17:10.150 --> 17:15.760
brackets and this not only holds far lists but also for another sequences or other data types which

17:15.760 --> 17:17.740
are sequences like strings.

17:17.740 --> 17:26.500
So if you create a string Doc dog is a sequence of three characters d o g.

17:27.170 --> 17:33.140
And we can also select here the first element at an exposition 0.

17:33.140 --> 17:38.870
So it's an d and we can also select the very last element G.

17:39.380 --> 17:44.940
And before we set that list mutable so we can change elements in our list.

17:45.410 --> 17:49.160
And that's the difference to strings so strings are immutable.

17:49.250 --> 17:58.100
If we want to assign a new value to let's say in exposition one let's say a stack instead of dark and

17:58.130 --> 18:00.350
then Python gives us an error message.

18:00.620 --> 18:02.220
So this is an example of that.

18:02.280 --> 18:06.510
String objects or as a sequence that is are immutable.

18:06.940 --> 18:07.340
All right.

18:07.340 --> 18:13.860
So now we have finished our session here and I hope you have a better understanding of lists and in

18:13.880 --> 18:21.980
our next session we are going to the next the data type which is lossless sequence tuples so happy to

18:21.980 --> 18:23.540
see you in the next session by.
